#include "pe_ccrgbaprotocol.h"
#include "CCQTSceneDirector.h"
#include "cocos2d.h"
using namespace cocos2d;


PE_CCRGBAProtocol::PE_CCRGBAProtocol(QtVariantPropertyManager *variantManager):PEditor(variantManager)
{
    
}

QtProperty *PE_CCRGBAProtocol::initUI()
{
    addProperty(QVariant::Color , "Color");
    
    return m_topItem;    
}

QColor PE_CCRGBAProtocol::qcolorFromCNode(CCRGBAProtocol * cnode)
{
	ccColor3B color = cnode->getColor();
	QColor qcolor(color.r,color.g,color.b) ;
	qcolor.setAlpha( cnode->getOpacity());
	return qcolor;
}

void PE_CCRGBAProtocol::refresh()
{
    CCRGBAProtocol * cnode = dynamic_cast<CCRGBAProtocol*>(CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
	if (cnode == NULL) return;
	Property("Color")->setValue(qcolorFromCNode(cnode));
}

void PE_CCRGBAProtocol::valueChanged(QtProperty *property, QVariant variant)
{
    CCRGBAProtocol * cnode = dynamic_cast<CCRGBAProtocol*>(CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
	if (cnode == NULL) return;

	if (property == Property("Color")) 
	{
		QColor color =variant.value<QColor>();
		cnode->setColor(ccc3(color.red() , color.green() , color.blue()));
		cnode->setOpacity(color.alpha());
	}
}
